package com.sigmatrix.h5.service.db;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sigmatrix.h5.config.TableUtils;
import com.sigmatrix.h5.entity.v2.DisDealer;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.mapper.DisDealerMapper;
import com.sigmatrix.h5.service.v2.CommonDBService;


/**
 * 
 * @author wei.wang
 *
 */
@Service
public class DisDealerDBService extends CommonDBService<DisDealerMapper, DisDealer> {

	/**
	 * 测试使用
	 * @param id
	 * @param tablePrefix
	 * @return
	 */
	public DisDealer selectOneBySQL(Long id, String tablePrefix) {
		return baseMapper.selectOneBySQL(id, tablePrefix);
	}

	
	/**
	 * 根据code获取DisDealer
	 * 
	 * @param tablePrefix
	 * @param code
	 * @return
	 */
	public DisDealer selectByCode(String eseCode,String code) {
		DisDealer dealer = new DisDealer();
		dealer.setTablePrefix(TableUtils.tablePrefixBy(eseCode));
		dealer.setCode(code);
		List<DisDealer> selectList = selectList(new EntityWrapper<DisDealer>(dealer));
		if(selectList!=null){
			if(selectList.size()>1){
				throw new MessageException("表disDealer.selectByCode查询发生多条数据。code:"+code);
			}
			if(!selectList.isEmpty()){
				return selectList.get(0);
			}
		}
		return null;
	}
	
	
	/**
	 * 查询所有的父经销商链。
	 * 
	 * @param id
	 * @param eseCode
	 * @return
	 */
	public List<DisDealer> findParentsById(Integer id,String eseCode){
		return baseMapper.findParentsById(id.longValue(), TableUtils.tablePrefixBy(eseCode));
	}
	
	/**
	 * 查询所有的父经销商链。
	 * 
	 * @param code
	 * @param eseCode
	 * @return
	 */
	public List<DisDealer> findParentsByCode(String code,String eseCode){
		return baseMapper.findParentsByCode(code, TableUtils.tablePrefixBy(eseCode));
	}
	
}